package com.ebn.mation.policymanagertest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import ga.mdm.PolicyManager;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "PolicyManagerTest";

    private PolicyManager mPolicyManager;
    private TextView mResultTextView;
    private ScrollView mScrollView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化视图
        initViews();

        // 获取PolicyManager实例
        initPolicyManager();

        // 设置按钮点击事件
        setupButtonListeners();
    }

    private void initViews() {
        mResultTextView = findViewById(R.id.result);
        mScrollView = findViewById(R.id.scrollview);
    }

    private void initPolicyManager() {
        try {
            mPolicyManager = PolicyManager.getInstance();

            if (mPolicyManager != null) {
                appendResult("成功获取 PolicyManager 实例");
                Log.d(TAG, "成功获取 PolicyManager 实例");
            } else {
                appendResult("获取 PolicyManager 实例失败");
                Log.e(TAG, "获取 PolicyManager 实例失败");
            }
        } catch (Exception e) {
            String error = "初始化 PolicyManager 失败: " + e.getMessage();
            appendResult(error);
            Log.e(TAG, error, e);
        }
    }

    private void setupButtonListeners() {
        // 测试WLAN策略
        findViewById(R.id.testWlan).setOnClickListener(v -> testWlanPolicies());

        // 测试数据连接策略
        findViewById(R.id.testData).setOnClickListener(v -> testDataConnectivityPolicies());

        // 测试蓝牙策略
        findViewById(R.id.testBluetooth).setOnClickListener(v -> testBluetoothPolicies());

        // 测试NFC策略
        findViewById(R.id.testNfc).setOnClickListener(v -> testNfcPolicies());

        // 测试红外策略
        findViewById(R.id.testIr).setOnClickListener(v -> testIrPolicies());

        // 测试生物识别策略
        findViewById(R.id.testBiometric).setOnClickListener(v -> testBiometricPolicies());
    }

    private void testWlanPolicies() {
        appendResult("\n===== 测试WLAN策略 =====");
        if (mPolicyManager == null) {
            appendResult("PolicyManager 实例为空");
            return;
        }

        try {
            // 测试设置WLAN策略
            boolean setResult = mPolicyManager.setWlanPolicies(1);
            appendResult("setWlanPolicies(1) 结果: " + (setResult ? "成功" : "失败"));

            // 测试获取WLAN策略
            int getResult = mPolicyManager.getWlanPolicies();
            appendResult("getWlanPolicies() 结果: " + getResult);
        } catch (Exception e) {
            appendResult("WLAN策略测试失败: " + e.getMessage());
            Log.e(TAG, "WLAN策略测试失败", e);
        }
    }

    private void testDataConnectivityPolicies() {
        appendResult("\n===== 测试数据连接策略 =====");
        if (mPolicyManager == null) {
            appendResult("PolicyManager 实例为空");
            return;
        }

        try {
            boolean setResult = mPolicyManager.setDataConnectivityPolicies(2);
            appendResult("setDataConnectivityPolicies(2) 结果: " + (setResult ? "成功" : "失败"));

            int getResult = mPolicyManager.getDataConnectivityPolicies();
            appendResult("getDataConnectivityPolicies() 结果: " + getResult);
        } catch (Exception e) {
            appendResult("数据连接策略测试失败: " + e.getMessage());
            Log.e(TAG, "数据连接策略测试失败", e);
        }
    }

    private void testBluetoothPolicies() {
        appendResult("\n===== 测试蓝牙策略 =====");
        if (mPolicyManager == null) {
            appendResult("PolicyManager 实例为空");
            return;
        }

        try {
            String[] testList = new String[]{"test_device_1", "test_device_2"};
            boolean setResult = mPolicyManager.setBluetoothPolicies(3, testList);
            appendResult("setBluetoothPolicies(3, testList) 结果: " + (setResult ? "成功" : "失败"));

            String[] getResult = mPolicyManager.getBluetoothPolicies();
            if (getResult != null) {
                StringBuilder sb = new StringBuilder();
                for (String item : getResult) {
                    sb.append(item).append(", ");
                }
                appendResult("getBluetoothPolicies() 结果: " + sb.toString());
            } else {
                appendResult("getBluetoothPolicies() 结果: null");
            }
        } catch (Exception e) {
            appendResult("蓝牙策略测试失败: " + e.getMessage());
            Log.e(TAG, "蓝牙策略测试失败", e);
        }
    }

    private void testNfcPolicies() {
        appendResult("\n===== 测试NFC策略 =====");
        if (mPolicyManager == null) {
            appendResult("PolicyManager 实例为空");
            return;
        }

        try {
            boolean setResult = mPolicyManager.setNfcPolicies(4);
            appendResult("setNfcPolicies(4) 结果: " + (setResult ? "成功" : "失败"));

            int getResult = mPolicyManager.getNfcPolicies();
            appendResult("getNfcPolicies() 结果: " + getResult);
        } catch (Exception e) {
            appendResult("NFC策略测试失败: " + e.getMessage());
            Log.e(TAG, "NFC策略测试失败", e);
        }
    }

    private void testIrPolicies() {
        appendResult("\n===== 测试红外策略 =====");
        if (mPolicyManager == null) {
            appendResult("PolicyManager 实例为空");
            return;
        }

        try {
            boolean setResult = mPolicyManager.setIrPolicies(5);
            appendResult("setIrPolicies(5) 结果: " + (setResult ? "成功" : "失败"));

            int getResult = mPolicyManager.getIrPolicies();
            appendResult("getIrPolicies() 结果: " + getResult);
        } catch (Exception e) {
            appendResult("红外策略测试失败: " + e.getMessage());
            Log.e(TAG, "红外策略测试失败", e);
        }
    }

    private void testBiometricPolicies() {
        appendResult("\n===== 测试生物识别策略 =====");
        if (mPolicyManager == null) {
            appendResult("PolicyManager 实例为空");
            return;
        }

        try {
            boolean setResult = mPolicyManager.setBiometricRecognitionPolicies(6);
            appendResult("setBiometricRecognitionPolicies(6) 结果: " + (setResult ? "成功" : "失败"));

            int getResult = mPolicyManager.getBiometricRecognitionPolicies();
            appendResult("getBiometricRecognitionPolicies() 结果: " + getResult);
        } catch (Exception e) {
            appendResult("生物识别策略测试失败: " + e.getMessage());
            Log.e(TAG, "生物识别策略测试失败", e);
        }
    }

    // 在结果区域添加文本并滚动到底部
    private void appendResult(String text) {
        runOnUiThread(() -> {
            mResultTextView.append(text + "\n");
            mScrollView.fullScroll(View.FOCUS_DOWN);
        });
    }
}
